package Verktoy;

import Beans.Lop;
import DB.DBehandler;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;


/**
 * Klasse som konverterer løp mellom string og object
 * @author Gruppe1
 */
@ManagedBean(name="LopConverter")
public class LopConverter implements Converter{
    private ArrayList<Lop> lop;
    private DBehandler db = new DBehandler();
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String [] tab = value.split(" ");
        lop = db.finnAlleLop();
        for(Lop l : lop){
            if(Integer.parseInt(tab[tab.length-1]) == l.getLop_id()){
                return new Lop(l);
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Lop l = (Lop) value;
        return l.toString();
    }
}
